/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Hogar;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Fluces extends Canvas implements CommandListener{
private Ticker tick;
private Command atras;
private int[][] coordLuz;
private boolean[] estLuz;
private Image planta, encendida, apagada, selec1, selec2;
private Hogar midlet;
private int luzSeleccionada;
public Fluces(Hogar midlet){
this.midlet = midlet; //Guardo la referencia del MIDlet
planta = midlet.edificio.getPlanta();
luzSeleccionada = 0;
tick = new Ticker("Iluminacion");
atras = new Command("Atras",Command.BACK,1);
this.setTicker(tick);
this.addCommand(atras);
this.setCommandListener(this);
try{
encendida = Image.createImage("/luzencendida.png");
apagada = Image.createImage("/luzapagada.png");
selec1 = Image.createImage("/luzselec1.png");
selec2 = Image.createImage("/luzselec2.png");
}catch (Exception e){
System.out.println(e);
}
}
public void paint(Graphics g){
g.drawImage(planta,0,0,Graphics.TOP|Graphics.LEFT);
dibujarLuces(g);
}
public void dibujarLuces(Graphics g){
for (int i=0;i<midlet.edificio.getNumLuces();i++){
if (luzSeleccionada == i){
if (estLuz[i]) g.drawImage(selec2,coordLuz[i][0],coordLuz[i][1],
Graphics.TOP|Graphics.LEFT);
else g.drawImage(selec1,coordLuz[i][0],coordLuz[i][1],
Graphics.TOP|Graphics.LEFT);
}else{
if (estLuz[i]) g.drawImage(encendida,coordLuz[i][0],coordLuz[i][1],
Graphics.TOP|Graphics.LEFT);
else g.drawImage(apagada,coordLuz[i][0],coordLuz[i][1],
Graphics.TOP|Graphics.LEFT);
}
}
}
public void commandAction(Command c, Displayable d){
if (c == atras)
midlet.verOpciones();
}
public void keyPressed(int codigo){
int ncod = getGameAction(codigo);
switch (ncod){
case Canvas.FIRE:{
estLuz[luzSeleccionada] = !estLuz[luzSeleccionada];
midlet.edificio.setEstadoLuz(estLuz);
break;
}
case Canvas.RIGHT:{
if (luzSeleccionada < (midlet.edificio.getNumLuces()-1))
luzSeleccionada++;
break;
}
case Canvas.LEFT:{
if (luzSeleccionada > 0)
luzSeleccionada--;
break;
}
}
repaint();
}
public void setInfo1(boolean[] est){
estLuz = est;
}
public void setInfo2(int[][] coord){
coordLuz = coord;
}
}
        